package com.partners.utils;

import com.partners.constant.BusinessMessage;
import com.partners.constant.SuccessMessageEnum;
import com.partners.model.ApiResult;

/**
 * 响应模型工具类
 * @author liangyongle
 * @date 2018/8/14 0014
 */
public class ApiResultUtil {
    private static final Object EMPTY_DATA[] = {};

    public static<T> ApiResult<T> ok(T data) {
        ApiResult<T> result = new ApiResult<>();
        BusinessMessage ok = SuccessMessageEnum.OK;
        result.setCode(ok.getCode());
        result.setMsg(ok.getMsg());
        result.setData(data);
        return result;
    }

    public static<T> ApiResult<T> ok(BusinessMessage businessMessage, T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setCode(businessMessage.getCode());
        result.setMsg(businessMessage.getMsg());
        result.setData(data);
        return result;
    }

    public static ApiResult ok(BusinessMessage businessMessage) {
        return emptyData(businessMessage);
    }

    public static ApiResult failure(BusinessMessage businessMessage) {
        return emptyData(businessMessage);
    }

    public static ApiResult failure(String validMsg) {
        ApiResult<Object> result = new ApiResult<>();
        result.setCode(-10000);
        result.setMsg(validMsg);
        result.setData(EMPTY_DATA);
        return result;
    }

    private static ApiResult emptyData(BusinessMessage businessMessage) {
        ApiResult<Object> result = new ApiResult<>();
        result.setCode(businessMessage.getCode());
        result.setMsg(businessMessage.getMsg());
        result.setData(EMPTY_DATA);
        return result;
    }

}
